﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dropit.BlogPageProvider.Blog;

namespace Dropit.BlogPageProvider.WordPress
{
	public class Provider : IBlogProvider
	{
		private static Dictionary<int, JoeBlogs.WordPressWrapper> _connections = new Dictionary<int, JoeBlogs.WordPressWrapper>();

		public List<IBlog> GetBlogs(Connection connection)
		{
			return Blog.List(GetConnection(connection)).Select<Blog, IBlog>(blog => blog as IBlog).ToList();
		}

		public IBlog GetBlogById(Connection connection, string id)
		{
			WordPressId wordPressId;
			if (WordPressId.TryParse(id, out wordPressId))
			{
				return Blog.Load(GetConnection(connection), wordPressId);
			}
			return null;
		}

		public IComment GetCommentById(Connection connection, string id)
		{
			WordPressId wordPressId;
			if (WordPressId.TryParse(id, out wordPressId))
			{
				return Comment.Load(GetConnection(connection), wordPressId);
			}
			return null;
		}

		public List<IComment> GetComments(Connection connection, string id)
		{
			WordPressId wordPressId;
			if (WordPressId.TryParse(id, out wordPressId))
			{
				return Comment.List(GetConnection(connection), wordPressId).Select<Comment, IComment>(comment => comment as IComment).ToList();
			}
			return new List<IComment>(0);
		}

		public IPost GetPostById(Connection connection, string id)
		{
			WordPressId wordPressId;
			if (WordPressId.TryParse(id, out wordPressId))
			{
				return Post.Load(GetConnection(connection), wordPressId);
			}
			return null;
		}

		public List<IPost> GetPosts(Connection connection, string id)
		{
			WordPressId wordPressId;
			if (WordPressId.TryParse(id, out wordPressId))
			{
				return Post.List(GetConnection(connection), wordPressId).Select<Post, IPost>(post => post as IPost).ToList();
			}
			return new List<IPost>(0);
		}

		public static JoeBlogs.WordPressWrapper GetConnection(BlogPageProvider.Blog.Connection connection)
		{
			if (!_connections.ContainsKey(connection.Id))
			{
				_connections.Add(connection.Id, new JoeBlogs.WordPressWrapper(connection.Url, connection.Username, connection.Password));
			}
			return _connections[connection.Id];
		}

		public IPost CreatePost(Connection connection)
		{
			return new Post();
		}

		public IComment CreateComment(Connection connection)
		{
			return new Comment();
		}

		public string SavePost(Connection connection, string parentId, IPost post)
		{
			WordPressId wordPressId;
			if (post != null && WordPressId.TryParse(parentId, out wordPressId))
			{
				return Post.SaveNew(GetConnection(connection), wordPressId, post).WordPressId.ToString();
			}
			return string.Empty;
		}

		public string SaveComment(Connection connection, string parentId, IComment comment)
		{
			WordPressId wordPressId;
			if (comment!= null && WordPressId.TryParse(parentId, out wordPressId))
			{
				return Comment.SaveNew(GetConnection(connection), wordPressId, comment).WordPressId.ToString();
			}
			return string.Empty;
		}

		public void DeletePost(Connection connection, string id)
		{
			throw new NotImplementedException();
		}

		public void DeleteComment(Connection connection, string id)
		{
			throw new NotImplementedException();
		}

		public void UpdatePost(Connection connection, IPost post)
		{
			throw new NotImplementedException();
		}

		public void UpdateComment(Connection connection, IComment comment)
		{
			throw new NotImplementedException();
		}
	}
}
